<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./css/index.css" />
  </head>
  <body>
    <div class="container">
      <ul class="top">
        <!-- 基础模板 -->
        <!-- 顶级的 标题 -->
        <li>
          <a href="javascript:;">首页</a>
          <!-- 二级标题 -->
          <ul class="sub">
            <li>
              <a href="javascript:;">
                <span>砂锅厨具</span>
                <img
                  src="https://yanxuan.nosdn.127.net/3102b963e7a3c74b9d2ae90e4380da65.png?quality=95&imageView"
                  alt=""
                />
              </a>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <script src="./axios.js"></script>
    <script>
      // 1.打开页面调用

      window.onload = () => {
        axios({
          url: 'http://ajax-api.itheima.net/api/category/top',
          method: 'get',
        }).then((res) => {
          // console.log(res)
          const er = res.data.data.map((item) => {
            // console.log(item);
            return axios({
              url: 'http://ajax-api.itheima.net/api/category/sub',
              method: 'get',
              params: {
                id: item.id,
              },
            })
          })
          Promise.all(er).then((res) => {
            const liStr = res.map((item) => {
              console.log(item)
              return `<li>
          <a href="javascript:;">${item.data.data.name}</a>
          <!-- 二级标题 -->
          <ul class="sub">
            ${item.data.data.children
              .map((item) => {
                // console.log(item)
                return `<li>
              <a href="javascript:;">
                <span>${item.name}</span>
                <img src='${item.picture}' alt="" />
              </a>
            </li>
            `
              })
              .join('')}

          </ul>
        </li> `
            })
            document.querySelector('.top').innerHTML = liStr.join('')
          })
        })
      }
    </script>
  </body>
</html>
